﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aludra.Core.Models
{
    public class ExtraDeckZone
    {
        private List<MonsterCard> _extraDeck;

        #region IField Members
        public FieldType FieldType
        {
            get { return FieldType.GraveZone; }
        }

        /// <summary>
        /// In deck zone, the Size property indicates the original size of the deck
        /// </summary>
        public int Size
        {
            get { return int.MaxValue; }
        }

        public int Count
        {
            get { return _extraDeck.Count; }
        }

        public event EventHandler<CardsRemovedEventArgs> CardsRemoved;

        public event EventHandler<CardsAddedEventArgs> CardsAdded;

        #endregion

        public ExtraDeckZone(IEnumerable<MonsterCard> monsterCards)
        {
            _extraDeck = new List<MonsterCard>();
            foreach (MonsterCard card in monsterCards)
            {
                MonsterCategory mc = card.MonsterCategory;
                if (mc == MonsterCategory.Xyz || mc == MonsterCategory.Synchro
                    || mc == MonsterCategory.Fusion)
                {
                    _extraDeck.Add(card);
                }
            }
        }

        public void Remove(MonsterCard item)
        {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<CardsRemovedEventArgs> temp = CardsRemoved;
            if (temp != null)
            {
                temp(this, new CardsRemovedEventArgs(this.FieldType, new[] { item }));
            }
            _extraDeck.Remove(item);
        }

        public MonsterCard this[int index]
        {
            get
            {
                return _extraDeck[index];
            }
        }

        public void Add(MonsterCard item)
        {
            EventHandler<CardsAddedEventArgs> tempAdded = this.CardsAdded;
            if (tempAdded != null)
            {
                tempAdded(this, new CardsAddedEventArgs(this.FieldType, new[] { item }));
            }

            _extraDeck.Add(item);
        }

        public void Clear()
        {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<CardsRemovedEventArgs> temp = CardsRemoved;
            if (temp != null)
            {
                temp(this, new CardsRemovedEventArgs(this.FieldType, _extraDeck));
            }
            _extraDeck.Clear();
        }

        public bool Contains(MonsterCard item)
        {
            return _extraDeck.Contains(item);
        }
    }
}
